package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.IntemInformationMapper;
import com.kang.dao.ItemDetailMapper;
import com.kang.dao.ReturnRequestItemMapper;
import com.kang.dao.ReturnRequestMapper;
import com.kang.pojo.ItemDetail;
import com.kang.pojo.ReturnRequest;
import com.kang.pojo.ReturnRequestItem;
import com.kang.service.ReturnRequestService;
import com.vonechina.entity.page.ItemDetailPage;
import com.vonechina.entity.page.ReturnRequestPage;

/**
 * 退货申请服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class ReturnRequestServiceImpl implements ReturnRequestService {
	/**
	 * 退货申请持久层
	 */
	@Resource
	private ReturnRequestMapper rrm;
	/**
	 * 退货申请物品信息持久层
	 */
	@Resource
	private ReturnRequestItemMapper rrim;
	/**
	 * 物品批次持久层
	 */
	@Resource
	private ItemDetailMapper idm;
	/**
	 * 物品信息持久层
	 */
	@Resource
	private IntemInformationMapper im;

	/**
	 * 分页查询退货申请
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ReturnRequest> listReturnRequest(ReturnRequestPage page) {
		page.setRows(rrm.countReturnRequest(page));
		return rrm.listReturnRequest(page);
	}

	/**
	 * 生成退货申请编号
	 * 
	 * @return
	 */
	@Override
	public String generateReturnRequestCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = rrm.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "RR" + no;
	}

	/**
	 * 通过主键查询退货申请
	 * 
	 * @param returnRequestID
	 *            退货申请ID
	 * @return
	 */
	@Override
	public ReturnRequest selectReturnRequestByID(Integer returnRequestID) {
		return rrm.selectByPrimaryKey(returnRequestID);
	}

	/**
	 * 新增退货申请
	 * 
	 * @param request
	 *            退货申请雪铁龙
	 * @return
	 */
	@Override
	public int insertReturnRequest(ReturnRequest request) {
		return rrm.insertSelective(request);
	}

	/**
	 * 提交退货申请
	 * 
	 * @param request
	 *            退货申请实体类
	 * @return
	 */
	@Override
	public int submitReturnRequest(ReturnRequest request) {
		return rrm.submitByPrimaryKey(request);
	}

	/**
	 * 更新退货申请
	 * 
	 * @param request
	 *            退货申请实体类
	 * @return
	 */
	@Override
	public int updateReturnRequest(ReturnRequest request) {
		return rrm.updateByPrimaryKeySelective(request);
	}

	/**
	 * 删除退货申请
	 * 
	 * @param returnRequestID
	 *            退货申请ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer returnRequestID) {
		int a = rrm.deleteByPrimaryKey(returnRequestID);
		if (a > 0) {
			rrim.deleteByRequestID(returnRequestID);
		}
		return a;
	}

	/**
	 * 查询可退货物品批次
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<ItemDetail> listItemDetailForReturn(ItemDetailPage page) {
		page.setRows(idm.countItemNotIn(page));
		return idm.listItemNotIn(page);
	}

	/**
	 * 保存退货申请物品信息
	 * 
	 * @param returnRequestID
	 *            退货申请ID
	 * @param ids
	 *            退货申请批次ID
	 * @param numbers
	 *            退货申请物品数量
	 * @return
	 */
	@Override
	public Integer saveReturnItem(Integer returnRequestID, Integer[] ids, Integer[] numbers) {
		rrim.deleteNotInItem(returnRequestID, ids);
		for (int i = 0; i < ids.length; i++) {
			ReturnRequestItem item = rrim.selectItem(returnRequestID, ids[i]);
			if (item == null) {
				ItemDetail iDetail = idm.selectById(ids[i]);
				item = new ReturnRequestItem();
				item.setReturnRequestID(returnRequestID);
				item.setNumber(numbers[i]);
				item.setSourceID(ids[i]);
				item.setItemInformationID(iDetail.getItemInformationID());
				rrim.insertSelective(item);
			} else {
				item.setNumber(numbers[i]);
				rrim.updateByPrimaryKeySelective(item);
			}
		}
		return ids.length;
	}

	/**
	 * 查看退货申请物品信息
	 * 
	 * @param returnRequestID
	 *            退货申请ID
	 * @return
	 */
	@Override
	public List<ReturnRequestItem> listItemByReturnRequestID(Integer returnRequestID) {
		return rrim.listByReturnRequestID(returnRequestID);
	}

	/**
	 * 检查数量是否足够退货
	 * 
	 * @param ids
	 *            物品批次ID
	 * @param numbers
	 *            退货数量
	 * @return
	 */
	@Override
	public boolean checkItemNumberEnough(Integer[] ids, Integer[] numbers) {
		for (int i = 0; i < ids.length; i++) {
			ItemDetail iDetail = idm.selectById(ids[i]);
			if (numbers[i] > iDetail.getNumber()) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 减少库存
	 * 
	 * @param ids
	 *            物品picID
	 * @param numbers
	 *            数量
	 * @return
	 */
	@Override
	public int subtractStock(Integer[] ids, Integer[] numbers) {
		for (int i = 0; i < ids.length; i++) {
			ItemDetail iDetail = idm.selectById(ids[i]);
			idm.subtractStock(ids[i], numbers[i]);
			im.subtractStock(iDetail.getIntemInformationID(), numbers[i]);
		}
		return ids.length;
	}

	/**
	 * 更新退货申请状态
	 * 
	 * @param returnRequestID
	 *            退货申请ID
	 * @param status
	 *            状态
	 * @return
	 */
	@Override
	public int updateStatus(Integer returnRequestID, Integer status) {
		return rrm.updateStatus(returnRequestID, status);
	}

}
